package com.dnxh.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnxh.constant.CommonConstant;
import com.dnxh.constant.CustomResult;
import com.dnxh.constant.EmailEnum;
import com.dnxh.entity.Account;
import com.dnxh.entity.Department;
import com.dnxh.entity.common.PageContext;
import com.dnxh.entity.request.CreateAccountRequest;
import com.dnxh.entity.request.ModifyPassRequest;
import com.dnxh.entity.request.SearchAdminRequest;
import com.dnxh.mapper.AccountMapper;
import com.dnxh.service.AccountService;
import com.dnxh.service.DepartmentService;
import com.dnxh.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhiqiu
 * @since 2022-04-06
 */
@Slf4j
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
	
	@Resource
	private RedisUtil     redisUtil;
	@Resource
	private AccountMapper accountMapper;
	@Resource
	private DepartmentService departmentService;
	
	@Override
	public Account selectRoleByUserName (String userName) {
		return getOne (Wrappers.<Account>lambdaQuery ()
				.select (Account::getIdentity, Account::getPassWord)
				.eq (Account::getUserName, userName));
	}
	
	@Override
	public boolean verify (String userName, String password) {
		String psd = MD5.create ().digestHex16 (password);
		Account account = getOne (Wrappers.<Account>lambdaQuery ()
				.select (Account::getPassWord)
				.eq (Account::getUserName, userName));
		if (account == null) {
			return false;
		} else {
			return psd.equals (account.getPassWord ());
		}
	}
	
	@Override
	public CustomResult addAccount (CreateAccountRequest request) {
		String key = CommonConstant.SEND_MESSAGE + EmailEnum.LOGIN.name () + StrUtil.COLON + request.getEmail ();
		String code = redisUtil.get (key) + StrUtil.EMPTY;
		if (! request.getCode ().equals (code)) {
			return CustomResult.fail ("验证码错误!");
		}
		if (getOne (Wrappers.<Account>lambdaQuery ()
				.select (Account::getId)
				.eq (Account::getUserName, request.getUsername ())) != null) {
			return CustomResult.fail ("用户名不可重复!");
		}
		if (getOne (Wrappers.<Account>lambdaQuery ()
				.select (Account::getId)
				.eq (Account::getEmail, request.getEmail ())) != null) {
			return CustomResult.fail ("邮箱不可重复!");
		}
		save (new Account ()
				.setId (IdUtil.getSnowflakeNextId ())
				.setUserName (request.getUsername ())
				.setPassWord (MD5.create ().digestHex16 (request.getPassword ()))
				.setEmail (request.getEmail ())
				.setIdentity (CommonConstant.ADMIN)
				.setCreateTime (new Date ()));
		return CustomResult.success ();
	}
	
	@Override
	public CustomResult search (SearchAdminRequest request) {
		if (request.getPageSize () == - 1) {
			request.setPageSize ((int) count ());
		}
		IPage<Account> iPage = new Page<Account> ()
				.setSize (request.getPageSize ())
				.setCurrent (request.getPageNum ());
		if (CharSequenceUtil.isEmpty (request.getUser ())) {
			IPage<Account> noUserList = page (iPage);
			return CustomResult.success (new PageContext<Account> ()
					.setCount (noUserList.getTotal ())
					.setList (noUserList.getRecords ()));
		} else {
			IPage<Account> list = page (iPage, Wrappers.<Account>lambdaQuery ()
					.like (Account::getUserName, request.getUser ()));
			return CustomResult.success (new PageContext<Account> ()
					.setCount (list.getTotal ())
					.setList (list.getRecords ()));
		}
	}
	
	@Override
	public CustomResult deleteAccount (Long id) {
		accountMapper.del (id);
		return CustomResult.success ();
	}
	
	@Override
	public CustomResult modifyPassword (ModifyPassRequest request) {
		update (Wrappers.<Account>lambdaUpdate ()
				.set (Account::getPassWord, MD5.create ().digestHex16 (request.getPass ()))
				.eq (Account::getId, request.getId ()));
		return CustomResult.success ();
	}
	
	@Override
	public CustomResult addDepartment (String department) {
		departmentService.save (new Department ()
				.setId (IdUtil.getSnowflakeNextId ())
				.setDepartmentName (department));
		return CustomResult.success ();
	}
	
	@Override
	public CustomResult deleteDepartment (Long id) {
		departmentService.deleteDepartment (id);
		return CustomResult.success ();
	}
}
